<template>
	<view>
		<dsl-navbar title="紧急联系人" bgTopImg="white" isBack=""></dsl-navbar>
		<view style="padding-top: 10px;">
			<view class="beijing" :style="{height: windowHeight}">
				<view class="oneRow rowjbic">
					<view class="fs26 cccc">联系人姓名</view>
					<input type="text" placeholder="请输入姓名" v-model="name" />
				</view>
				<view class="oneRow rowjbic">
					<view class="fs26 cccc">联系人电话</view>
					<input type="text" placeholder="请输入电话" v-model="mobile" />
				</view>
				<view class="oneRow rowjbic">
					<view class="fs26 cccc">联系人地址</view>
					<input type="text" placeholder="请输入详细地址" v-model="address" />
				</view>
				<view class="rowjc" @click="addEmergencyContact">
					<view class="baocun">保存</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		addEmergencyContact,
		updateEmergencyContact
	} from '@/common/path.js'
	export default {
		data() {
			return {
				windowHeight: 0, //可用窗口的高度
				name: '',
				mobile: '',
				address: '',
				isUrgency:false,//是否修改
			}
		},
		onLoad(option) {
			if (option.data) {
				let obj = JSON.parse(option.data)
				this.name = obj.name
				this.mobile = obj.mobile
				this.address = obj.mobile
				this.isUrgency = true
				this.urgencyId = obj.id
			}
			console.log('[紧急联系人]', option);
		},
		onReady() {
			this.getWindowHeight()
		},
		methods: {
			getWindowHeight() {
				this.windowHeight = uni.getSystemInfoSync().windowHeight - 10 + 'px'
			},
			//添加紧急联系人
			addEmergencyContact() {
				if (this.name == '') {
					this.$tools.showToast('请填写联系人名称')
					return
				}
				if (this.mobile.length != 11) {
					this.$tools.showToast('请填写正确联系方式')
					return
				}
				if (this.address == '') {
					this.$tools.showToast('请填写联系人地址')
					return
				}
				let data = {
					name: this.name,
					mobile: this.mobile,
					address: this.address,
					userId: uni.getStorageSync('userInfo').id
				}
				let url =addEmergencyContact //默认添加
				if(this.isUrgency){
					data.id = this.urgencyId
					url = updateEmergencyContact
					
				}else{
					url = addEmergencyContact //添加
				}

				this.Http.POST(url, data)
					.then((res) => {
						console.log('[紧急联系人]', res);
						if (res.code == 0) {
							this.$tools.showToast2(res.msg)
							setTimeout(r => {
								uni.navigateBack({
									delta: 1
								})
							}, 1000)
						} else {
							this.$tools.showToast(res.msg)
						}
					})
			}
		}
	}
</script>

<style>
	.beijing {
		background-color: #FFFFFF;
		padding: 10rpx 20rpx 0rpx;
		box-sizing: border-box;
	}

	.oneRow {
		height: 100rpx;
		border-bottom: 1rpx solid #EEEEEE;
	}

	input {
		text-align: right;
		font-size: 28rpx;
		color: #333333;
	}

	.baocun {
		width: 500rpx;
		height: 80rpx;
		border-radius: 36rpx;
		background-color: #29EAC4;
		text-align: center;
		line-height: 80rpx;
		color: rgba(255, 255, 255, 0.7);
		font-size: 32rpx;
		position: absolute;
		bottom: 100rpx;
	}
</style>
